﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baidu.API.Entity {
    public class DicResult{
        public int errno;
        public DicData data;
        public string to;
        public string from;

        /// <summary>
        /// 返回是否翻译成功
        /// </summary>
        /// <returns></returns>
        public bool IsSuccess(){
            var result = true;
            try{
                var err = data.symbols[0];
            }
            catch (Exception){
                result = false;
            }
            
            return result;
        }

        /// <summary>
        /// 重载 ToString 翻译成功的话 返回翻译的字符串,失败返回数据类型
        /// </summary>
        /// <returns></returns>
        public override string ToString(){
            var resultString = new StringBuilder();
            if (IsSuccess()){
                foreach (var word in data.symbols[0].parts){
                    resultString.AppendFormat("{0} ", word.part);
                    foreach (var mean in word.means){
                        resultString.AppendFormat("{0},", mean);
                    }
                    resultString.Length -= 1;
                    resultString.AppendLine();
                }
            }
            else{
                resultString.AppendLine(this.GetType().ToString());
            }
            return resultString.ToString();
        }
    }

    public class DicData{
        public string word_name;
        public Symbols[] symbols;
    }

    public class Symbols{
        public string ph_zh;
        public Parts[] parts;
    }

    public class Parts{
        public string part;
        public string[] means;
    }
}
